 package msg.chestionar.ejb.beans;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.security.PermitAll;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import msg.chestionar.ejb.bi.ChestionarBI;

import com.msg.chestionar.entities.ChestionarE;
import com.msg.chestionar.entities.IntrebareE;
import com.msg.chestionar.entities.IntrebareOptiuneE;
import com.msg.chestionar.to.ChestionarTO;
import com.msg.chestionar.to.IntrebareOptiuneTO;
import com.msg.chestionar.to.IntrebareTO;

@Stateless
@PermitAll
public class ChestionarBA implements ChestionarBI {

	@PersistenceContext
	private EntityManager em;

	@Override
	public ChestionarTO getChestionar(Long id) {
		Query query = em.createNamedQuery("ChestionarE.findById");
		query.setParameter("id", id);

		if (!query.getResultList().isEmpty()) {
			ChestionarE chestionarE = (ChestionarE) query.getResultList().get(0);
			return chestionarE.getChestionarTO();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<ChestionarTO> getToateChestionarele() {

		Query query = em.createNamedQuery("ChestionarE.findAll");

		return getChestionarTOList(query.getResultList());
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<ChestionarTO> getToateChestionareleActive() {
		Query query = em.createNamedQuery("ChestionarE.findAllActiv");

		return getChestionarTOList(query.getResultList());
	}

	private List<ChestionarTO> getChestionarTOList(
			List<ChestionarE> chestionareE) {
		List<ChestionarTO> rezultat = new ArrayList<ChestionarTO>();
		for (ChestionarE chestionarE : chestionareE) {
			rezultat.add(chestionarE.getChestionarTO());
		}
		return rezultat;
	}

	@Override
	public ChestionarTO saveChestionar(ChestionarTO chestionar) {
		ChestionarE entity = null;
		if (chestionar.getId() == null) {
			entity = new ChestionarE();

			if (chestionar.getActiv())
				entity.setActiv(1);
			else
				entity.setActiv(0);
			entity.setCreat_de(chestionar.getCreat_de());
			entity.setNume(chestionar.getNume());

			em.persist(entity);
		} else {
			entity = this.em.find(ChestionarE.class, chestionar.getId());
			if (chestionar.getActiv())
				entity.setActiv(1);
			else
				entity.setActiv(0);
			entity.setNume(chestionar.getNume());
		}

		for (IntrebareTO intrebare : chestionar.getIntrebari()) {
			saveIntrebare(intrebare, entity);
		}

		em.flush();
		em.refresh(entity);

		return entity.getChestionarTO();
	}

	private void saveIntrebare(IntrebareTO intrebare, ChestionarE chestionar) {
		IntrebareE entity = null;
		if (intrebare.getId() == null) {
			entity = new IntrebareE();

			entity.setChestionarE(chestionar);
			entity.setText(intrebare.getText());
			entity.setTip(intrebare.getTip());

			em.persist(entity);

			chestionar.addIntrebare(entity);
		} else {
			entity = this.em.find(IntrebareE.class, intrebare.getId());

			if (intrebare.getMarkedForDelete()) {
				chestionar.removeIntrebare(entity);

				this.em.remove(entity);

				return;
			} else {
				entity.setTip(intrebare.getTip());
				entity.setText(intrebare.getText());
			}
		}

		for (IntrebareOptiuneTO optiune : intrebare.getOptiuni()) {
			this.saveOptions(optiune, entity);
		}
	}

	private void saveOptions(IntrebareOptiuneTO opt, IntrebareE intrebare) {
		IntrebareOptiuneE entity;
		if (opt.getId() == null) {
			entity = new IntrebareOptiuneE();
			entity.setIntrebareE(intrebare);
			entity.setText(opt.getText());
			if (opt.getE_cu_comentariu()) {
				entity.setE_cu_comentariu(1);
			} else {
				entity.setE_cu_comentariu(0);
			}

			em.persist(entity);
			intrebare.addOptiune(entity);
		} else {
			entity = this.em.find(IntrebareOptiuneE.class, opt.getId());

			if (opt.getMarkedForDelete()) {
				this.em.remove(entity);

				intrebare.removeOptiune(entity);
			} else {
				entity.setText(opt.getText());
				if (opt.getE_cu_comentariu()) {
					entity.setE_cu_comentariu(1);
				} else {
					entity.setE_cu_comentariu(0);
				}
			}
		}
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<ChestionarTO> getChestionarPtrUser(Long userId) {
		// TODO Paula: Trebuie Query nou daca nu exista raspuns ptr userId;
		Query query = em.createNamedQuery("ChestionarE.findAllActiv");
		query.setParameter("loggedInUser", userId);

		return getChestionarTOList(query.getResultList());
	}

	@Override
	public void deleteChestionar(Long chestionarID) {
		// aici sa sterg si raspunsurile
		ChestionarE chestionarE = this.em.find(ChestionarE.class, chestionarID);
		this.em.remove(chestionarE);
		
	}

}
